package com.jumi.microservice.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.redis.RedisCache;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.MemberImInfoRedisKey;
import com.jumi.microservice.domain.dto.AccountDTO;
import com.jumi.microservice.domain.vo.AccountAuthVO;
import com.jumi.microservice.dto.reponse.JmUserReponse;
import com.jumi.microservice.service.AccountService;
import com.jumi.microservice.service.JmUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/auth/ImAuth")
@Api(tags = "IM客服权限相关接口")
public class ImAuthController extends BaseController {

    @Resource
    private AccountService accountService;

    @DubboReference
    private JmUserService jmUserService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisCache redisCache;

    private static final String key = "MemberImInfo::";

    @ApiOperation(value = "获取客服权限列表",notes = "获取客服权限列表")
    @GetMapping("/getImAuth")
    public ResponseResult<AccountAuthVO> getImAuth(){
        Long adminID = getUid();
        //查询账户信息
        AccountDTO accountDTO = accountService.getAccountByUid(adminID);
        //查询账户权限信息
        String auth = accountService.listImPriority(adminID);
        AccountAuthVO accountAuthVO = new AccountAuthVO();
        accountAuthVO.setId(accountDTO.getId());
        accountAuthVO.setUid(accountDTO.getUid());
        accountAuthVO.setUserAccount(accountDTO.getUserAccount());
        accountAuthVO.setUserName(accountDTO.getUserName());
        accountAuthVO.setMobile(accountDTO.getMobile());
        accountAuthVO.setUserAvatar(accountDTO.getUserAvatar());
        accountAuthVO.setImAuthVO(auth);
        return ResultGenerator.genSuccessResult(accountAuthVO);
    }

    @ApiOperation(value = "批量查询用户信息",notes = "批量查询用户信息")
    @GetMapping("/getMemberInfo")
    public ResponseResult getMemberInfo(String uids){
        //根据uid判断是否存在redis
        String[] uidString = uids.split(",");
        List<Integer> noIds = new ArrayList<>();
        List<JmUserReponse> jmuserreponse = new ArrayList<>();
        for (int i = 0;i < uidString.length;i++){
            JmUserReponse jmUserReponse = redisCache.getCacheObject(
                    MemberImInfoRedisKey.MEMBER_IM_INFO+Integer.parseInt(uidString[i]));
            if(jmUserReponse==null){
                noIds.add(Integer.parseInt(uidString[i]));
            }else{
                //添加返回数据
                jmuserreponse.add(jmUserReponse);
            }
        }
        if(CollectionUtil.isNotEmpty(noIds)){
            Integer[] ids = new Integer[]{noIds.size()};
            ResponseResult<List<JmUserReponse>> response = jmUserService.getUsersInfoDetail(noIds.toArray(ids));
            for (JmUserReponse jmUserReponse : response.getData()) {
                //存入redis
                redisAll(jmUserReponse);
                //添加返回数据
                jmuserreponse.add(jmUserReponse);
            }
        }
        return ResultGenerator.genSuccessResult(jmuserreponse);
    }


    private void redisAll(JmUserReponse jmUserReponse){
        redisCache.setCacheObject(MemberImInfoRedisKey.MEMBER_IM_INFO+jmUserReponse.getId(), jmUserReponse, 10, TimeUnit.MINUTES);
    }
}
